<template>
  <!-- 三级联动导航 -->
  <div class="type-nav">
    <div class="container">
      <!-- 事件委托/事件委派 -->
      <div @mouseleave="leaveIndex" @mouseenter="enterShow">
        <h2 class="all">全部商品分类</h2>
        <!-- 过渡动画 -->
        <transition name="sort">
          <div class="sort" v-show="showNav">
            <div class="all-sort-list2" @click="toSearch">
              <div
                class="item"
                v-for="(categoryItem, index) in categoryList"
                :key="categoryItem.categoryId"
                :class="{ active: currentIndex === index }"
              >
                <h3 @mouseenter="changeIndex(index)">
                  <a
                    :data-categoryName="categoryItem.categoryName"
                    :data-categroy1id="categoryItem.categoryId"
                    >{{ categoryItem.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div
                    class="subitem"
                    v-for="subItem of categoryItem.categoryChild"
                    :key="subItem.categoryId"
                  >
                    <dl class="fore">
                      <dt>
                        <a
                          :data-categoryName="subItem.categoryName"
                          :data-categroy2id="subItem.categoryId"
                          >{{ subItem.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="childItem of subItem.categoryChild"
                          :key="childItem.categoryId"
                        >
                          <a
                            :data-categoryName="childItem.categoryName"
                            :data-categroy3id="childItem.categoryId"
                            >{{ childItem.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useStore } from 'vuex'
// 按需引入节流
import throttle from 'lodash/throttle'
import { useRoute, useRouter } from 'vue-router'
// 引入vuex的实例store
const store = useStore()
// 引入 router
const router = useRouter()
// 引入route
const route = useRoute()

/** 三级联动的显示与隐藏的控制 */
const showNav = ref(true)

// 发请求常写在mounted生命周期钩子中
onMounted(() => {
  // 判断是否在 home 页面，不在home页面就将 三级联动菜单隐藏
  if (route.path !== '/home') {
    showNav.value = false
  }
})
// 获取请求到的三级联动列表
const categoryList = store.state.typeNav.categoryList.slice(0, 16)

// 事件委托：鼠标移入的处理
const enterShow = () => {
  if (route.path !== '/home') {
    // 鼠标移入展示商品列表（三级联动）
    showNav.value = true
  }
}

// 背景色的处理
// 存储鼠标移入的那一个分类的index
const currentIndex = ref(-1)
// 鼠标移入修改 currentIndex
// 函数节流
const changeIndex = throttle((index) => {
  currentIndex.value = index
}, 50)

// 事件委托：鼠标移除的处理
const leaveIndex = () => {
  // 鼠标移除，重置currentIndex
  currentIndex.value = -1
  if (route.path !== '/home') {
    // 鼠标移除，隐藏三级联动列表
    showNav.value = false
  }
}

// 跳转到搜索页
const toSearch = (e) => {
  // 获取到所以子节点
  const element = e.target
  // 通过dataset属性判断节点是否带有自定义属性，即a标签
  const { categoryname, categroy1id, categroy2id, categroy3id } =
    element.dataset
  if (categoryname) {
    // 跳转时传的参数
    const location = {
      name: 'search'
    }
    const query = {
      categoryName: categoryname
    }
    // 判断是1级、2级、还是3级下的a标签
    if (categroy1id) {
      // 添加参数ID
      query.category1Id = categroy1id
    } else if (categroy2id) {
      query.category2Id = categroy2id
    } else {
      query.category3Id = categroy3id
    }
    // 合并query参数
    location.query = query
    // 判断是否有params参数
    if (route.params) {
      location.params = route.params
    }
    // 路由跳转
    router.push(location)
  }
}
</script>
<style lang="scss" scoped>
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          &.active {
            background-color: skyblue;
          }
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
    // 过渡动画样式
    // 过渡动画开始（进入）样式
    .sort-enter-from {
      height: 0px;
    }
    // 过渡动画结束样式
    .sort-enter-to {
      height: 461px;
    }
    // 定义动画时间、速率
    .sort-enter-active {
      transition: all 0.5s linear;
    }
  }
}
</style>
